<?php
namespace app\common\validate;

use think\Validate;

class AccountMoney extends Validate
{
    protected $rule = [
        'mobile'  =>  'require|regex:/^1[3-9]\d{9}$/|checkExists|token',
        'recharge_money'  =>  'require|egt:10|float',
        'give_money'  =>  'float|elt:recharge_money|egt:0',
        'consumption_money'  =>  'number|egt:0|consumption_check',
        'usepaycode'  => 'checkPayCode',
        'diy_times'  => 'number|egt:0|consumption_check',
        'fish_times'  => 'number|egt:0|consumption_check',
        'rollball_times'  => 'number|egt:0|consumption_check',
        'deduction'       => 'deductionCheck',
    ];

    protected $message  =   [
        'recharge_money.require' => '请选择充值金额',
        'recharge_money.egt' => '充值金额不得少于10元',
        'recharge_money.float' => '充值金额有误',
        'give_money.float' => '赠送金额有误',
        'give_money.elt' => '赠送金额不得大于充值金额',
        'give_money.egt' => '赠送金额不得小于0',
        'mobile.regex' => '非法的手机号',
        'diy_times.number' => 'DIY请填写合法的次数',
        'diy_times.egt' => 'DIY次数不得小于0',
        'fish_times.number' => '吃奶鱼请填写合法的次数',
        'fish_times.egt' => '吃奶鱼次数不得小于0',
        'rollball_times.number' => '滚球请填写合法的次数',
        'rollball_times.egt' => '滚球次数不得小于0',
        'consumption_money.number' => '消费金额有误',
    ];

    protected $scene = [
        // 充值场景
        'recharge'  =>  ['mobile', 'amount_money', 'recharge_money', 'give_money'],
        // 次数充值场景
        'times_recharge'  =>  ['mobile', 'diy_times', 'fish_times', 'rollball_times', 'deduction'],
        // 消费场景
        'consumption'  =>  ['mobile','consumption_money', 'usepaycode', 'diy_times', 'fish_times', 'rollball_times'],
        // 修改场景
        'edit'  =>  ['mobile','recharge_money', 'give_money', 'diy_times', 'fish_times', 'rollball_times'],
    ];

    // 自定义验证规则
    protected function checkExists($value,$rule,$data) {
        $accountmoney_model = model('AccountMoney');
        return $accountmoney_model->where('mobile', '=', $value)->count() ? true : '会员不存在';
    }

    // 会员消费，检测消费码是否有效
    protected function checkPayCode($value,$rule,$data) {
        $sms_model = new \app\common\model\SmsMobile;
        $validate_res = $sms_model->validateCode($data['mobile'], $data['pay_code'], 'paycode');

        return $validate_res['state'] ? true : $validate_res['msg'];
    }

    protected function consumption_check($value,$rule,$data) {

        if(isset($data['consumption_money'])) 
            return (!$data['consumption_money'] && !$data['fish_times'] && !$data['diy_times'] && !$data['rollball_times']) ? '四项收费不能同时为0' : true;
        else
            return (!$data['fish_times'] && !$data['diy_times'] && !$data['rollball_times']) ? '三个项目不能同时为0' : true;
    }

    /*
     * 次数充值，余额检查
     */
    protected function deductionCheck($value,$rule,$data) {
        $money_model = \app\back\model\AccountMoney::where('mobile', $data['mobile'])->find();

        if(is_null($money_model))
            return '余额检测失败';

        if(!is_numeric($data['deduction_money']) || $data['deduction_money'] < 0)
            return '扣除金额有误';

        if($money_model->recharge_money < $data['deduction_money']) {
            return '帐户余额不足';
        }

        return true;
    }
}
